package com.ibm.cloud.banking.sample.service.impl;

import com.ibm.cloud.banking.sample.convert.OeRequestLogMapper;
import com.ibm.cloud.banking.sample.dto.OeRequestLogDTO;
import com.ibm.cloud.banking.sample.entity.OeRequestLogEntity;
import com.ibm.cloud.banking.sample.repository.OeRequestLogRepository;
import com.ibm.cloud.banking.sample.service.IPcsLogService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import javax.annotation.Resource;

/**
 * replace desc in here
 *
 * @author HanDong
 * @date 2022/7/26
 */
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = RuntimeException.class)
public class PcsLogService implements IPcsLogService {

    @Resource
    private OeRequestLogRepository oeRequestLogRepository;

    @Resource
    private OeRequestLogMapper oeRequestLogMapper;


    @Override
    public OeRequestLogDTO saveLog(OeRequestLogDTO oeRequestLogDTO) {
        return oeRequestLogMapper.entityToDto(
                oeRequestLogRepository.save(oeRequestLogMapper.dtoToEntity(oeRequestLogDTO)));
    }

    @Override
    public void updateLog(String id, String response) {
        Optional<OeRequestLogEntity> optionalOeRequestLogEntity = oeRequestLogRepository.findById(id);
        if (optionalOeRequestLogEntity.isPresent()) {
            OeRequestLogEntity oeRequestLogEntity = optionalOeRequestLogEntity.get();
            oeRequestLogEntity.setResponse(response);
            oeRequestLogRepository.save(oeRequestLogEntity);
        }
    }
}
